<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.queryParams.pageNum=1;this.pagination.current=1;this.getList()}"
            @reset="()=>{this.queryParams.pageNum=1;this.pagination.current=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="项目名称" name="projectName">
            <t-input
              v-model="queryParams.projectName"
              clearable
              placeholder="请输入项目名称"
              style="width: 240px"
            />
          </t-form-item>
<!--          <t-form-item label="变更类型" name="subjectClassification">-->
<!--            <t-select style="width: 240px" clearable v-model="queryParams.changeType"-->
<!--                      :options="dict.type.change_type">-->
<!--            </t-select>-->
<!--          </t-form-item>-->
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit">查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <t-tabs v-if="site_tag === 'jtt'" @change="(val)=>{queryParams.projectCategory=val;getList();}" v-model="queryParams.projectCategory" style="margin-bottom: 10px">
      <t-tab-panel :key="item.id" :value="item.id" v-for="(item) in [{'name': '全部',id: null},...projectCategoryOptions]" :label="item.name" :destroyOnHide="false">
      </t-tab-panel>
    </t-tabs>
    <t-table
      rowKey="id"
      :data="data"
      :columns="columns"
      hover
      :pagination="pagination"
      :loading="dataLoading"
    >
      <template #[`contract.projectName`]="{ row }">
        <t-link v-if="row.version === '2024'" @click="dialog.objId=row.projectId;dialog.header=row.contract.projectName;dialog.open_item_detail=true" hover="color"
                theme="primary">{{ row.contract.projectName }}
        </t-link>
        <t-link v-else @click="dialog.objId=row.projectId;dialog.header=row.contract.projectName;dialog.open_project_detail=true" hover="color"
                theme="primary">{{ row.contract.projectName }}
        </t-link>
      </template>
      <template #status="{ row }">
        <t-tag @click="dialog.header=row.contract.projectName;dialog.objId=row.projectId;dialog.open_approve_list=true" class="cursor-pointer" v-if="row.status" :theme="handleTheme(row.status,dict.type.approve_status)" variant="light">
          {{ row.status | filterByDict(dict.type.approve_status) }}
        </t-tag>
        <t-tag @click="dialog.header=row.contract.projectName;dialog.objId=row.projectId;dialog.open_approve_list=true" class="cursor-pointer" v-else theme="warning" variant="light">未提交</t-tag>
      </template>
      <template #[`projectAnnualReportTable.startDate`]="{row, rowIndex}">
        {{ row?.projectAnnualReportTable?.startDate?.substring(0, 4) }}年
        {{ row?.projectAnnualReportTable?.startDate?.substring(5, 7) }}月 至 {{ row?.projectAnnualReportTable?.endDate?.substring(0, 4) }}年
        {{ row?.projectAnnualReportTable?.endDate?.substring(5, 7) }}月
      </template>

      <template #[`projectAnnualReportTable.progress`]="{row, rowIndex}">
        {{ row?.projectAnnualReportTable?.progress | filterByDict(dict.type.progress) }}
      </template>
      <template #[`projectAnnualReportTable.mainReason`]="{row, rowIndex}">
        {{ row?.projectAnnualReportTable?.mainReason | filterByDict(dict.type.main_reason) }}
      </template>
      <template #[`projectAnnualReportTable.adjustContent`]="{row, rowIndex}">
        {{ row?.projectAnnualReportTable?.adjustContent | filterByDict(dict.type.adjust_content) }}
      </template>
      <template #[`projectAnnualReportTable.projectAnnualReportFunding.totalMoney`]="{row, rowIndex}">
        {{ row.projectAnnualReportTable?.projectAnnualReportFunding?.totalMoney }}万元
      </template>
      <template #[`projectAnnualReportTable.projectAnnualReportFunding.alreadyMoney`]="{row, rowIndex}">
        {{ row.projectAnnualReportTable?.projectAnnualReportFunding?.alreadyMoney }}万元
      </template>
      <template #[`projectAnnualReportTable.projectAnnualReportFunding.notMoney`]="{row, rowIndex}">
        {{ row.projectAnnualReportTable?.projectAnnualReportFunding?.notMoney }}万元
      </template>
      <template #[`projectAnnualReportTable.projectAnnualReportFunding.dependMoney`]="{row, rowIndex}">
        {{ row.projectAnnualReportTable?.projectAnnualReportFunding?.dependMoney }}万元
      </template>
      <template #[`projectAnnualReportTable.projectAnnualReportFunding.implementMoney`]="{row, rowIndex}">
        {{ row.projectAnnualReportTable?.projectAnnualReportFunding?.implementMoney }}万元
      </template>
      <template #op="{row, rowIndex}">
        <t-button v-hasPermi="['*:*:*']" @click="toUpdate(row)" size="small"
                  variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          编辑
        </t-button>
        <t-button @click="dialog.header=row.contract.projectName;dialog.objId=row.id;dialog.projectId=row.projectId;dialog.open_detail=true" size="small"  variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          详情
        </t-button>
              <t-button @click="createPDF(row)" size="small"
                        variant="text" class="t-button-link">
                <t-icon name="file-pdf" slot="icon"/>
                导出PDF
              </t-button>
      </template>
    </t-table>
    <project-detail-dialog tab-default-value="3" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_project_detail"
                           v-if="dialog.open_project_detail"></project-detail-dialog>
    <project-detail-dialog tab-default-value="2" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_item_detail"
                           v-if="dialog.open_item_detail"></project-detail-dialog>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list" v-if="dialog.open_approve_list"></approve-list>
    <performance-report-detail-dialog :header="dialog.header" :objId="dialog.objId" :project-id="dialog.projectId" :visible.sync="dialog.open_detail" v-if="dialog.open_detail"></performance-report-detail-dialog>

  </div>
</template>

<script>
import projectDetailDialog from "../pm/components/project-detail-dialog.vue";
import ItemDetailDialog from "../pm/components/item-detail-dialog.vue";
import ApproveList from "../approve/components/approve-list.vue";
import performanceReportDetailDialog from "./components/performance-report-detail-dialog.vue";

export default {
  name: "performance-report-index",
  dicts: ['approve_status' ,'progress', 'adjust_content', 'main_reason', 'degree', 'prof_tit'],
  components:{
    projectDetailDialog,
    ApproveList,
    performanceReportDetailDialog,
    ItemDetailDialog
  },
  data(){
    return {
      site_tag: import.meta.env.VITE_SITE_TAG,
      projectCategoryOptions: [],
      dialog: {
        open_project_detail: false,
        open_approve_list: false,
        header: "",
        objId: null,
        open: false,
        open_detail: false,
        open_item_detail: false,
      },
      data: [],
      columns: [
        // {
        //   colKey: "project.projectNum",
        //   title: "项目编号",
        //   align: "center",
        //   width: 120,
        // },
        {
          colKey: "contract.projectName",
          title: "项目名称",
          align: "center",
          width: 220
        },
        {
          colKey: "projectAnnualReportTable.reportDate",
          title: "报告日期",
          align: "center",
          width: 120
        },
        {
          colKey: "projectAnnualReportTable.startDate",
          title: "本报告期的起止时间",
          align: "center",
          width: 230
        },
        {
          colKey: "projectAnnualReportTable.progress",
          title: "进展情况",
          align: "center",
        },
        // {
        //   colKey: "projectAnnualReportTable.mainReason",
        //   title: "主要原因",
        //   align: "center",
        // },
        {
          colKey: "projectAnnualReportTable.adjustContent",
          title: "项目调整内容",
          align: "center",
        },
        {
          colKey: "projectAnnualReportTable.projectAnnualReportFunding.totalMoney",
          title: "拨款总经费",
          align: "center",
        },
        {
          colKey: "projectAnnualReportTable.projectAnnualReportFunding.alreadyMoney",
          title: "已拨经费",
          align: "center",
        },
        {
          colKey: "projectAnnualReportTable.projectAnnualReportFunding.notMoney",
          title: "未拨经费",
          align: "center",
        },
        {
          colKey: "projectAnnualReportTable.projectAnnualReportFunding.dependMoney",
          title: "依托工程配套总经费",
          align: "center",
        },
        {
          colKey: "projectAnnualReportTable.projectAnnualReportFunding.implementMoney",
          title: "已经落实配套经费",
          align: "center",
        },
        // {
        //   colKey: "createTime",
        //   title: "创建时间",
        //   align: "center",
        // },
        {
          colKey: "status",
          title: "审批状态",
          align: "center",
          width: 180,
          fixed: 'right'
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 180,
          fixed: 'right'
        },
      ],
      dataLoading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: '',
        changeType: '',
        projectCategory: null
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10,20,30],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  created(){
    this.getList();
    this.getDeclareTree();
  },
  methods:{
    getDeclareTree() {
      this.$api.kjxm.project.planCategoryList().then(response => {
        this.projectCategoryOptions = this.handleTree(response.data, "id");
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    createPDF(row){
      const instance = this.$loading({
        fullscreen: true,
        attach: 'body',
        preventScrollThrough: false,
      });
      this.$api.kjxm.implement.performanceReportPDF(row.id).then(response => {
        window.open(response.data.fullFilePath, '_blank')
        instance.hide();
      }).catch((e) => {
        instance.hide();
        this.$message.error(e.toString());
      });
    },
    getList(){
      this.dataLoading = true;
      this.$api.kjxm.implement.performanceReportList(this.queryParams).then(response => {
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    toUpdate(row){
      sessionStorage.removeItem(`${row.id}-performance-report-activeForm`)
      const taskId = row.id;
      const activeFormList = [];
      this.$api.kjxm.implement.getPerformanceReportAll(row.id).then(response => {
        sessionStorage.removeItem(`${taskId}-performance-report-step0`);
        sessionStorage.removeItem(`${taskId}-performance-report-projectId`);
        if(response.data.id){
          sessionStorage.setItem(`${taskId}-performance-report-step0`, response.data.id);
          sessionStorage.setItem(`${taskId}-performance-report-projectId`, row.projectId);
        }else {
          activeFormList.push(0);
        }
        if (response.data.projectAnnualReportPlan && response.data.projectAnnualReportPlan.id) {
          sessionStorage.setItem(`${taskId}-performance-report-step1`, response.data.projectAnnualReportPlan.id)
        } else {
          activeFormList.push(1);
        }
        if (response.data.projectAnnualReportGetResults && response.data.projectAnnualReportGetResults.id) {
          sessionStorage.setItem(`${taskId}-performance-report-step2`, response.data.projectAnnualReportGetResults.id)
        } else {
          activeFormList.push(2);
        }
        if (response.data.projectAnnualReportExperience && response.data.projectAnnualReportExperience.id) {
          sessionStorage.setItem(`${taskId}-performance-report-step3`, response.data.projectAnnualReportExperience.id)
        } else {
          activeFormList.push(3);
        }
        if (response.data.projectAnnualReportTable && response.data.projectAnnualReportTable.id) {
          sessionStorage.setItem(`${taskId}-performance-report-step4`, response.data.projectAnnualReportTable.id)
        } else {
          activeFormList.push(4);
        }
        if (activeFormList.length <= 0) {
          sessionStorage.setItem(`${taskId}-performance-report-activeForm`, 4); // 跳转到最后一步
        } else {
          sessionStorage.setItem(`${taskId}-performance-report-activeForm`, activeFormList[0]); // 跳转到第一个不满足
        }
        this.$router.push({path:`/kjxm/other/implement/performance-report-apply/${row.id}`,query:{title:`${row.contract.projectName}-项目执行情况表`}});
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    toApply(){
      this.$router.push({
        path: `/kjxm/other/implement/performance-report-apply/${Date.parse(new Date())}`,
      })
    },
  }
}
</script>

<style scoped>

</style>
